package org.testis.wirelesssecuritymodule.core;

import org.testis.data.AtofModuleConstants;
import org.testis.logging.AtofLogLevel;
import org.testis.logging.LogSenderService;
import org.testis.utils.StringUtils;
import org.testis.utils.TerminalApplicationExecutor;
import org.testis.wirelesssecuritymodule.data.WirelessSecurityModuleDataObject;

/**
 *
 * @author nurhat
 */
public class WlanApiProvider extends WirelessConnectionSupplier {

    TerminalApplicationExecutor m_termApp;
    TerminalApplicationExecutor m_createProfileTermApp;

    public WlanApiProvider(WirelessSecurityModuleDataObject dObj) {
        super(dObj);
    }

    @Override
    public void afterExecute() {
        if (m_createProfileTermApp != null) {
            LogSenderService.putFileMessageOnQueue("WlanApiProvider->ProfileCreate:",
                    AtofLogLevel.DEBUG,
                    "\n Exit Code:\n"
                    + m_createProfileTermApp.getExitValue()
                    + "\nOutput:\n" + m_createProfileTermApp.getOutputString()
                    + "\n Error:\n" + m_createProfileTermApp.getErrorString(),
                    getDobj().getMessageId());
        }
        if (m_termApp != null) {

            LogSenderService.putFileMessageOnQueue("WlanApiProvider->Connect:",
                    AtofLogLevel.DEBUG,
                    "\n Exit Code:\n"
                    + m_termApp.getExitValue()
                    + "\nOutput:\n" + m_termApp.getOutputString()
                    + "\n Error:\n" + m_termApp.getErrorString(),
                    getDobj().getMessageId());
        }
    }

    public void run() {
        try {
            String createProfilecommand = getSetProfileCommandToRun();
            String command = getCommandToRun();
            m_createProfileTermApp = new TerminalApplicationExecutor(createProfilecommand);
            m_createProfileTermApp.runExec();
            System.out.println("createProfilecommand:" + createProfilecommand);
            System.out.println("command:" + command);
            m_termApp = new TerminalApplicationExecutor(command);
            m_termApp.runExec();
        } catch (InterruptedException ex) {
            throw new WirelessSecurityModuleException(
                    WirelessSecurityModuleExceptionType.UNEXPLAINED,
                    "Execution of WlanApiProvider:Wireless connection interrupted due to timeout..."
                    + ex.getMessage());
        } catch (Exception ex) {
            throw new WirelessSecurityModuleException(
                    WirelessSecurityModuleExceptionType.UNEXPLAINED,
                    "Execution of WlanApiProvider:Wireless connection run failed..."
                    + ex.getMessage());
        } finally {
            m_termApp.endExec();
        }
    }

    private String getSetProfileCommandToRun() throws Exception {
        String profileFolder = AtofModuleConstants.ATOF_USER_TEMP_DIRECTORY + "/"
                + StringUtils.removeSpaces(getDobj().getParamInterface());
        String profileFile = profileFolder + "/" + getDobj().getWpaSsid()
                + ".xml";

        StringBuffer cmd = new StringBuffer();
        cmd.append("Wlsample sp ");

        if (getNicGuid() != null && !getNicGuid().isEmpty()) {
            cmd.append(" " + getNicGuid().replace("{", "").replace("}", ""));
        } else {
            throw new Exception(getDobj().getParamInterface() + " interface not found");
        }
        cmd.append(" \"" + profileFile+"\"");
        return cmd.toString();
    }

    private String getCommandToRun() throws Exception {
        StringBuffer cmd = new StringBuffer();
        cmd.append("Wlsample conn");

        if (getNicGuid() != null && !getNicGuid().isEmpty()) {
            cmd.append(" " + getNicGuid().replace("{", "").replace("}", ""));
        } else {
            throw new Exception(getDobj().getParamInterface() + " interface not found");
        }
        cmd.append(" " + getDobj().getWpaSsid() + " i " + getDobj().getWpaSsid());
        return cmd.toString();
    }
}
